import http from '@ohos.net.http';

export default class HttpUtil {

  static singleInstance: HttpUtil;
  private httpRequest: http.HttpRequest = http.createHttp();

  private baseUrl: string = "http://www.baidu.com";
  private httpProtocol: http.HttpProtocol = http.HttpProtocol.HTTP1_1;

  private constructor() {
  }

  public static getInstance() {
    if (HttpUtil.singleInstance) {
      return HttpUtil.singleInstance;
    }
    HttpUtil.singleInstance = new HttpUtil();
    return HttpUtil.singleInstance;
  }

  public setBaseUrl(baseUrl: string) {
    this.baseUrl = baseUrl;
  }

  public getBaseUrl() {
    return this.baseUrl;
  }

  public setHttpProtocol(httpProtocol: http.HttpProtocol) {
    this.httpProtocol = httpProtocol;
  }

  public getHttpProtocol() {
    return this.httpProtocol
  }

  public setListener():void {
    this.httpRequest.on("headersReceive", (headers)=> {
      console.log(`===>header: ${JSON.stringify(headers)}`)
    });
    let res = "";
    this.httpRequest.on("dataReceive", (data: ArrayBuffer) => {
      res += data;
      console.log(`===>res: ${res}`)
    });
    this.httpRequest.on("dataEnd", () => {
      console.info('===>No more data in response, data receive end');
    })
    this.httpRequest.on("dataReceiveProgress", (data) => {
      console.log("===>dataReceiveProgress receiveSize:" + data.receiveSize + ", totalSize:" + data.totalSize);
    })
  }

  public removeListener():void {
    this.httpRequest.off("headersReceive", (header)=> {
      console.log(`===>header: ${JSON.stringify(header)}`)
    });
    let res = "";
    this.httpRequest.off("dataReceive", (data: ArrayBuffer) => {
      res += data;
      console.log(`===>res: ${res}`)
    });
    this.httpRequest.off("dataEnd", () => {
      console.info('===>No more data in response, data receive end');
    });
    this.httpRequest.off("dataReceiveProgress", (data) => {
      console.log("===>dataReceiveProgress receiveSize:" + data.receiveSize + ", totalSize:" + data.totalSize);
    });
  }

  public async get(requestPath: string, options?: http.HttpRequestOptions): Promise<http.HttpResponse> {
    let mOptions: http.HttpRequestOptions;
    if (options) {
      mOptions = options
    }else {
      mOptions = {
        method: http.RequestMethod.GET,
        usingProtocol: this.httpProtocol
      }
    };
    let url: string = this.baseUrl + requestPath;
    let response: http.HttpResponse = await this.httpRequest.request(url, mOptions);
    if (response.responseCode != 0) {
      return response;
    }
    return response;
  }

  public async post(requestPath: string, data: string| Object| ArrayBuffer, options?: http.HttpRequestOptions)
    : Promise<http.HttpResponse> {
    let mOptions: http.HttpRequestOptions;
    if (options) {
      mOptions = options
    }else {
      mOptions = {
        method: http.RequestMethod.POST,
        extraData: data
      }
      let url: string = this.baseUrl + requestPath;
      let response: http.HttpResponse = await this.httpRequest.request(url, mOptions);
      if (response.responseCode != 0) {
        return response;
      }
      return response;
    }
  }
}

